﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Controls.Primitives;
using System.Timers;

namespace ArmoryViewer
{
	/// <summary>
	/// Interaction logic for PreferencesWindow.xaml
	/// </summary>
	public partial class PreferencesWindow : Window
	{
        /// <summary>
        /// Initializes a new instance of the <see cref="PreferencesWindow"/> class.
        /// </summary>
		public PreferencesWindow()
		{
			this.InitializeComponent();
            Settings.Load();
		}

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            txtArmoryUrl.Text = Settings.ArmoryUrl.ToString();
            chkHideWhenMinimized.IsChecked = Settings.HideWhenMinimized;
            chkCheckForUpdates.IsChecked = Settings.UpdateOnStartup;
            chkDownloadSockets.IsChecked = Settings.DownloadSockets;
            chkHandleAVProtocol.IsChecked = Settings.HandleAvProtocol;
            chkHandleAVProtocol.IsEnabled = Settings.IsAdmin;
            chkDownloadAchievements.IsChecked = Settings.DownloadAchievements;
            switch (Settings.Language)
            {
                case "www":
                    cboLanguage.SelectedItem = "English";
                    break;
                case "de":
                    cboLanguage.SelectedItem = "Deutsch";
                    break;
                case "es":
                    cboLanguage.SelectedItem = "Español";
                    break;
                case "fr":
                    cboLanguage.SelectedItem = "Français";
                    break;
                case "ru":
                    cboLanguage.SelectedItem = "Русском";
                    break;
                default:
                    cboLanguage.SelectedItem = "English";
                    break;
            }

            // Proxy
            switch (Settings.Proxy.ProxyType)
            {
                case Settings.ProxyType.AutoDetect: rdoAutoDetectProxy.IsChecked = true; break;
                case Settings.ProxyType.Manual: rdoManualProxy.IsChecked = true; break;
                case Settings.ProxyType.None: rdoNoProxy.IsChecked = true; break;
            }
            txtProxyAddress.Text = Settings.Proxy.Address;
            txtProxyPort.Text = Settings.Proxy.Port;
            txtProxyUsername.Text = Settings.Proxy.Username;
            txtProxyPassword.Password = Settings.Proxy.Password;
        }

		private void RadioButton_Checked(object sender, System.Windows.RoutedEventArgs e)
		{
			proxyConfig.IsEnabled = true;
		}

		private void rdoNoProxy_Checked(object sender, System.Windows.RoutedEventArgs e)
		{
            if (proxyConfig != null)
			    proxyConfig.IsEnabled = false;
		}

		private void rdoAutoDetect_Checked(object sender, System.Windows.RoutedEventArgs e)
		{
			if (proxyConfig != null)
			    proxyConfig.IsEnabled = false;
		}

        private void btnSave_Click(object sender, RoutedEventArgs e)
        {
            try { Settings.ArmoryUrl = new Uri(txtArmoryUrl.Text); }
            catch (UriFormatException) 
            { 
                txtArmoryUrl.BorderBrush = new SolidColorBrush(Colors.Red);
                txtArmoryUrl.Background = new SolidColorBrush(Colors.Pink);
                MessageBox.Show("Armory Url Invalid");
                txtArmoryUrl.SelectAll();
                txtArmoryUrl.Focus();
                return; 
            }
            Settings.HideWhenMinimized = (bool)chkHideWhenMinimized.IsChecked;
            Settings.UpdateOnStartup = (bool)chkCheckForUpdates.IsChecked;
            Settings.DownloadSockets = (bool)chkDownloadSockets.IsChecked;
            Settings.HandleAvProtocol = (bool)chkHandleAVProtocol.IsChecked;
            Settings.DownloadAchievements = (bool)chkDownloadAchievements.IsChecked;

            string language = "www";
            switch (cboLanguage.SelectedItem.ToString())
            {
                case "English":
                    language = "www";
                    break;
                case "Deutsch":
                    language = "de";
                    break;
                case "Español":
                    language = "es";
                    break;
                case "Français":
                    language = "fr";
                    break;
                case "Русском":
                    language = "ru";
                    break;
                default:
                    language = "www";
                    break;
            }
            Settings.Language = language;

            // Proxy
            if ((bool)rdoAutoDetectProxy.IsChecked) Settings.Proxy.ProxyType = Settings.ProxyType.AutoDetect;
            else if ((bool)rdoManualProxy.IsChecked) Settings.Proxy.ProxyType = Settings.ProxyType.Manual;
            else Settings.Proxy.ProxyType = Settings.ProxyType.None;
            Settings.Proxy.Address = txtProxyAddress.Text;
            Settings.Proxy.Port = txtProxyPort.Text;
            Settings.Proxy.Username = txtProxyUsername.Text;
            Settings.Proxy.Password = txtProxyPassword.Password;

            Settings.Save();
            Close();
        }

        private void txtArmoryUrl_TextChanged(object sender, TextChangedEventArgs e)
        {
            if (txtProxyAddress != null) txtArmoryUrl.BorderBrush = txtProxyAddress.BorderBrush;
            txtArmoryUrl.Background = new SolidColorBrush(Colors.White);
        }

        private void txtArmoryUrl_GotFocus(object sender, RoutedEventArgs e)
        {
            txtArmoryUrl.SelectAll();
        }
	}
}